// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const db = cloud.database()
  const orders = db.collection('orders')
  const openid = wxContext.OPENID

  try {
    const { orderId } = event
    if (!orderId) {
      return { success: false, message: '订单ID不能为空' }
    }
    // 只允许删除自己的订单
    const orderRes = await orders.doc(orderId).get()
    if (!orderRes.data || orderRes.data._openid !== openid) {
      return { success: false, message: '无权删除该订单' }
    }
    await orders.doc(orderId).remove()
    return { success: true, message: '订单已删除' }
  } catch (error) {
    console.error('删除订单失败：', error)
    return { success: false, message: '删除失败' }
  }
} 